/**
 * json请求*/
import axios from 'axios'
import { getToken } from '@/utils/auth'
import store from '@/store'
import { Toast, Dialog } from 'vant'

const service2 = axios.create({
  baseURL: process.env.VUE_APP_JSH_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 120000 // request timeout
})

service2.interceptors.request.use(
  config => {
    // do something before request is sent
    // config.headers['Content-Type'] = 'application/json'
    const token = "aHdvcmsmMzk3MWRoOTc1M2s3NTQzOWQy"
    if (token) {
      config.headers['Authorization'] = token
    }

    if (process.env.NODE_ENV === 'stage' || process.env.NODE_ENV === 'production') {
      config.url = config.url.replace('/system/test', '/system')
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service2.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.log(error.response)
    const err = error.response.data
    // Toast(err.message)
    Dialog.alert({
      title: '提示',
      message: err.message
    }).then(() => {
      // on close
    })
    return Promise.reject(error.response)
  }
)
export default service2
